package cn.com.xinjinfu.pdoulcProduct.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.adapter.PagerAdapter;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.fragment.AboutPDou.AboutFragment;
import cn.com.xinjinfu.pdoulcProduct.fragment.AboutPDou.CommonProblemFragment;
import cn.com.xinjinfu.pdoulcProduct.fragment.AboutPDou.SalfFragment;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.utils.NewUtil;
import cn.com.xinjinfu.pdoulcProduct.view.CustomViewPager;
import cn.com.xinjinfu.pdoulcProduct.view.LoadStatusBox;

/**
 * Created by android-adt on 2016/4/8.
 */
public class AboutPdouAct extends BaseActivity implements View.OnClickListener{

    private TextView tv_about_pdou,tv_safety_control,tv_common_problem;
    RelativeLayout top_layout;
    CustomViewPager mViewPager;
    private LoadStatusBox loadbox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_about_pdou);
        TitleManager.showTitleWhite(this, "关于P兜", "");
        initView();
        Intent intent = getIntent();
        String CurrentItem = intent.getStringExtra("CurrentItem");
        if (CurrentItem.equals("aboutpdou")){
            mViewPager.setCurrentItem(0, true);
            TitleManager.showTitleWhite(this,"关于P兜","");
            top_layout.setBackgroundResource(R.drawable.img_aboutus);
        }else if (CurrentItem.equals("safl")){
            mViewPager.setCurrentItem(1, true);
            TitleManager.showTitleWhite(this,"安全保障","");
            top_layout.setBackgroundResource(R.drawable.img_aqbz);
        }else {
            mViewPager.setCurrentItem(2, true);
            TitleManager.showTitleWhite(this,"常见问题","");
            top_layout.setBackgroundResource(R.drawable.img_question);
        }


    }

    private void initView() {
        top_layout = (RelativeLayout) findViewById(R.id.top_layout);
        tv_about_pdou = (TextView) findViewById(R.id.tv_about_pdou);
        tv_safety_control = (TextView) findViewById(R.id.tv_safety_control);
        tv_common_problem = (TextView) findViewById(R.id.tv_common_problem);

        tv_about_pdou.setOnClickListener(this);
        tv_safety_control.setOnClickListener(this);
        tv_common_problem.setOnClickListener(this);


        mViewPager = (CustomViewPager)findViewById(R.id.view_pager);
        mViewPager.setScanScroll(true);//禁止左右滑动
        PagerAdapter mPagerAdapter = new PagerAdapter(this);

        /***
         * 此处可以用一个类，创建3个对象添加进来
         */
        mPagerAdapter.addTab(AboutFragment.class, null);
        mPagerAdapter.addTab(SalfFragment.class, null);
        mPagerAdapter.addTab(CommonProblemFragment.class, null);
        mViewPager.setAdapter(mPagerAdapter);

        loadbox = (LoadStatusBox) findViewById(R.id.loadStatusBox);
        loadbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                netCheck();
            }
        });
        loadbox.loading();
    }

    private void netCheck(){
        if (!DataHandler.isNetworkConnected(this)) {
            ToastManager.show(this, "请检查网络连接");
            loadbox.failed();
            return;
        }else {
            loadbox.success();
        }
    }

    /**
     * 改版后，分成3个页面，下面的切换菜单已经取消
     * @param v
     */
    @Override
    public void onClick(View v) {
        if (!NewUtil.checkClick(v.getId())) return;
        switch (v.getId()) {
            case R.id.tv_about_pdou:
                mViewPager.setCurrentItem(0, true);
                TitleManager.showTitleWhite(this,"关于P兜","");
                top_layout.setBackgroundResource(R.drawable.img_aboutus);
                tv_about_pdou.setVisibility(View.GONE);
                tv_safety_control.setVisibility(View.VISIBLE);
                tv_common_problem.setVisibility(View.VISIBLE);

                break;
            case R.id.tv_safety_control:
                mViewPager.setCurrentItem(1, true);
                TitleManager.showTitleWhite(this,"安全保障","");
                top_layout.setBackgroundResource(R.drawable.img_aqbz);
                tv_about_pdou.setVisibility(View.VISIBLE);
                tv_safety_control.setVisibility(View.GONE);
                tv_common_problem.setVisibility(View.VISIBLE);

                break;
            case R.id.tv_common_problem:
                mViewPager.setCurrentItem(2, true);
                TitleManager.showTitleWhite(this,"常见问题","");
                top_layout.setBackgroundResource(R.drawable.img_question);
                tv_about_pdou.setVisibility(View.VISIBLE);
                tv_safety_control.setVisibility(View.VISIBLE);
                tv_common_problem.setVisibility(View.GONE);

                break;
            default:
                break;
        }
    }

}
